﻿/********************************************************************************
     File:																
            DataProvider.cs                         
     Description:
            抽象数据操作类
     Author:									
            DDBuildTools
            http://DDBuildTools.supesoft.com
     Finish DateTime:
			2011-10-15 22:58:27
     History:
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using FrameWork.Components;
using repproblem.Components;

namespace repproblem.Data
{
    /// <summary>
    /// 委托将DataReader转为实体类
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public delegate T PopulateDelegate<T>(IDataReader dr);

    /// <summary>
    /// 数据抽象类
    /// </summary>
    public abstract partial class DataProvider
    {
        
        #region "DataProvider Instance"

        private static DataProvider _defaultInstance = null;
        static DataProvider()
        {
            DataProvider dataProvider;
            if (FrameWork.Common.GetDBType == "MsSql")
                dataProvider = new SqlDataProvider();
            else if (FrameWork.Common.GetDBType == "Access")
                dataProvider = new AccessDataProvider();
            else
                throw new ApplicationException("数据库配置不对！");
            _defaultInstance = dataProvider;
        }

        /// <summary>
        /// 数据访问抽象层实例
        /// </summary>
        /// <returns></returns>
        public static DataProvider Instance()
        {

            return _defaultInstance;
        }

        /// <summary>
        /// 数据访问抽象层实例
        /// </summary>
        /// <param name="strConn">数据库连接字符串</param>
        /// <returns></returns>
        public static DataProvider Instance(string strConn)
        {
            return new SqlDataProvider(strConn);
        }

        #endregion

        #region "its_RepProblem() - DataProvider"
        /// <summary>
        /// 新增/删除/修改 its_RepProblemEntity ()
        /// </summary>
        /// <param name="fam">its_RepProblemEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public abstract Int32 its_RepProblemInsertUpdateDelete(its_RepProblemEntity fam);
        /// <summary>
        /// 返回its_RepProblemEntity实体类的List对象 ()
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>its_RepProblemEntity实体类的List对象()</returns>
        public abstract List<its_RepProblemEntity> its_RepProblemList(QueryParam qp, out int RecordCount);

        /// <summary>
        /// 将记录集转为its_RepProblemEntity实体类 ()
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <returns>its_RepProblemEntity</returns>
        protected its_RepProblemEntity Populateits_RepProblemEntity(IDataReader dr)
        {
            its_RepProblemEntity nc = new its_RepProblemEntity();
            if (!Convert.IsDBNull(dr["ID"])) nc.ID = Convert.ToInt32(dr["ID"]); // 
            if (!Convert.IsDBNull(dr["ServRepProb"])) nc.ServRepProb = Convert.ToInt32(dr["ServRepProb"]); // 通用报修问题
            if (!Convert.IsDBNull(dr["Context"])) nc.Context = Convert.ToString(dr["Context"]).Trim(); // 问题描述
            if (!Convert.IsDBNull(dr["ServTrobTypeId"])) nc.ServTrobTypeId = Convert.ToInt32(dr["ServTrobTypeId"]); // 问题分类
            if (!Convert.IsDBNull(dr["ServProbTypeId"])) nc.ServProbTypeId = Convert.ToInt32(dr["ServProbTypeId"]); // 故障分类
            if (!Convert.IsDBNull(dr["ResolventId"])) nc.ResolventId = Convert.ToInt32(dr["ResolventId"]); // 通用解决方法
            return nc;
        }
        #endregion
        
        #region "获取表中字段值"
        /// <summary>
        /// 获取表中字段值(非安全函数,传入参数请进行Sql字符串过滤)
        /// </summary>
        /// <param name="table_name">表名</param>
        /// <param name="table_fileds">字段</param>
        /// <param name="where_fileds">查询条件字段</param>
        /// <param name="where_value">查询值</param>
        /// <returns></returns>
        public abstract string get_table_fileds(string table_name, string table_fileds, string where_fileds, string where_value);
        #endregion

        #region "列新表中字段值"
        /// <summary>
        /// 更新表中字段值(非安全函数,传入参数请进行Sql字符串过滤)
        /// </summary>
        /// <param name="Table">表名</param>
        /// <param name="Table_FiledsValue">需要更新值(不用带Set)</param>
        /// <param name="Wheres">更新条件(不用带Where)</param>
        /// <returns></returns>
        public abstract int Update_Table_Fileds(string Table, string Table_FiledsValue, string Wheres);
        #endregion
    }
}
